package com.bit.springblogdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bit.springblogdemo.common.constant.Constant;
import com.bit.springblogdemo.common.exception.BlogException;
import com.bit.springblogdemo.common.util.BeanTransUtils;
import com.bit.springblogdemo.mapper.BlogInfoMapper;
import com.bit.springblogdemo.pojo.dataobject.BlogInfo;
import com.bit.springblogdemo.pojo.request.AddBlogRequest;
import com.bit.springblogdemo.pojo.request.UpdateBlogRequest;
import com.bit.springblogdemo.pojo.response.BlogInfoResponse;
import com.bit.springblogdemo.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constant.BLOG_NORMAL);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream()
                .map(bolginfo -> BeanTransUtils.trans(bolginfo))
                .collect(Collectors.toList());
        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanTransUtils.trans(getBlogInfo(blogId));
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constant.BLOG_NORMAL)
                .eq(BlogInfo::getId, blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest, blogInfo);
        try{
            Integer result = blogInfoMapper.insert(blogInfo);
            if(result == 1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("博客插入失败");
            throw new BlogException("内部错误,请联系管理员");
        }
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.trans(updateBlogRequest);
       try {
           Integer result =  blogInfoMapper.updateById(blogInfo);
           return result==1;
       }catch (Exception e){
           log.error("更新博客失败");
           throw  new BlogException("内部错误,请联系管理员");
       }

    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constant.BLOG_DELETE);
        try{
            Integer result = blogInfoMapper.updateById(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("博客删除失败, e", e);
            throw new BlogException("内部错误,请联系管理员");
        }
    }
}
